#!/bin/bash

read -p "Enter Your Mysql Password: " MYSQL_PASS
read -p "Repeat Your Mysql Password: " MYSQL_PASS2
if [ ${MYSQL_PASS} != ${MYSQL_PASS2} ]; then
     echo "Your Passwords do not match!"
     exit
fi

# 安装
yum install -y mysql-devel mysql-server

# 系统服务
chkconfig --level 345 mysqld on

# 数据目录
mkdir -p /styx/dbroot
chown -R mysql /styx/dbroot

# 配置mysql服务器
cat > /etc/my.cnf <<EOF
[client]
port            = 3306
socket          = /usr/tmp/mysql.sock

[mysqld]
port            = 3306
socket          = /usr/tmp/mysql.sock

skip-locking
key_buffer = 16M
max_allowed_packet = 1M
table_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
skip-federated
log-bin=mysql-bin
server-id       = 1

datadir         = /styx/dbroot
basedir         = /usr/
tmpdir          = /tmp
pid-file        = /var/run/mysqld/mysqld.pid
default-time-zone     = '+8:00'
character-set-server  = utf8
collation-server      = utf8_general_ci
skip-name-resolve
skip-bdb
long_query_time = 1
log-slow-queries = slow.log
log_error = /var/log/mysql_error.log
skip-external-locking
myisam-recover=BACKUP
max_binlog_size = 100M

innodb_data_file_path = ibdata1:10M:autoextend
innodb_buffer_pool_size = 16M
innodb_log_file_size = 5M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
expire_logs_days        = 30

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[isamchk]
key_buffer = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[myisamchk]
key_buffer = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

EOF
################################################################

/usr/bin/mysql_install_db --user=mysql
/usr/bin/mysqld_safe --user=mysql &

# 设置MySql root密码
mysql -uroot mysql -e "UPDATE user SET Password=PASSWORD('${MYSQL_PASS}') WHERE User='root';DELETE
FROM user WHERE User='';UPDATE user SET Host='%' WHERE Host='localhost';FLUSH PRIVILEGES;"

